package org.example.controller;

import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.ReturnType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @FileName TestController
 * @Description
 * @Author coderfun
 * @selfDesc coding in fun
 * @date 2024-11-05
 **/
@RestController
@RequestMapping("/test")
public class TestLuaController {

    @Resource
    private RedisConnectionFactory redisConnectionFactory;

    @PostMapping("/lua")
    public void executeLuaScript(String[] args) {
        RedisConnection connection = redisConnectionFactory.getConnection();
        String luaScript = "return redis.call('GET', KEYS[1])";

        Object result = connection.eval(luaScript.getBytes(), ReturnType.VALUE, 1, "demodemo".getBytes());

        if (result instanceof byte[]) {
            result = new String((byte[]) result);
        }

        System.out.println(result);

        connection.close();
    }
}
